<?php

class Page_History extends X3_Module_Table {

    public $encoding = 'UTF-8';
    /*
     * uncomment if want new model functional
     */
    public $tableName = 'page_history';
    
    public $_fields = array(
        'id'=>array('integer[10]','unsigned','primary','auto_increment'),
        'page_id'=>array('integer[10]','unsigned','index','default'=>'NULL','ref'=>array('Page','id','default'=>'title')),
        'title'=>array('string[255]'),
        'model'=>array('string[255]'),
        'data'=>array('fulltext'),
        'created_at'=>array('integer[10]','unsigned','default'=>'0'),
    );
    
    /**
     * 
     * @param X3_Module_Table $model
     * @param string $title
     */
    public static function add(X3_Module_Table $model,$title='No name'){
        $el = new self();
        $el->page_id = $model->id;
        $el->title = $title;
        $el->model = get_class($model);
        $el->data = serialize($model->getTable()->getAttributes());
        $el->created_at = time();
        $el->save();
    }
    
    public static function restore($id){
        $el = self::getByPk($id);
        $model = X3_Module_Table::getByPk($el->page_id,$el->model);
        if($model == null)
            $model = new $el->model;
        $data = unserialize($el->data);
        $model->getTable()->acquire($data);
        if(!$model->save())
            throw new X3_Exception(X3::db()->getErrors());
        return $model;
    }

    public function beforeValidate() {
        if($this->created_at == 0)
            $this->created_at = time();
    }
    public static function newInstance($class=__CLASS__) {
        return parent::newInstance($class);
    }
    public static function getInstance($class=__CLASS__) {
        return parent::getInstance($class);
    }
    public static function get($arr=array(),$single=false,$class=__CLASS__) {
        return parent::get($arr,$single,$class);
    }
    public static function getByPk($pk,$class=__CLASS__) {
        return parent::getByPk($pk,$class);
    }
    public static function update($fields, $params = array(), $class = __CLASS__) {
        return parent::update($fields, $params, $class);
    }
    public static function delete($params = array(), $class = __CLASS__) {
        return parent::delete($params, $class);
    }
    public static function deleteByPk($pk, $class = __CLASS__) {
        return parent::deleteByPk($pk, $class);
    }
    public static function num_rows($params = array(), $class = __CLASS__) {
        return parent::num_rows($params, $class);
    }
}
?>
